package com.aerozhonghuan.logisticsmall.modules.order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.Group;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarFragment;
import com.aerozhonghuan.logisticsmall.modules.order.beans.StoreBean;
import com.aerozhonghuan.zh_map.map.ZHMapUtils;
import com.aerozhonghuan.zh_map.map.ZHMapView;
import com.aerozhonghuan.zh_map.map.bean.MapPointBean;
import com.blankj.utilcode.util.LogUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 描述 网点位置
 *
 * @author zhangyonghui
 * 创建日期 2019-05-25
 */
public class StationLocationFragment extends TitlebarFragment implements ZHMapView.OnMapLoadedCallback, View.OnClickListener {
    private View rootView;
    private MapPointBean mapPoint;
    private ZHMapView mapView;
    private Group mGroupWhenDownPull;
    private TextView mTvName;
    private TextView mTvDistance;
    private TextView mTvPhoneNumber;
    private TextView mTvAddress;
    private ImageView mIvStore;
    private StoreBean.StoreInfo mStoreInfo;
    private Group mGroupStoreinfo;
    private Bundle arguments;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_station_location, null);
            arguments = getArguments();
            if (arguments != null) {
                if (arguments.containsKey("StoreInfo")) {
                    mStoreInfo = (StoreBean.StoreInfo) arguments.getSerializable("StoreInfo");
                }
                initView();
                initData();
            } else {
                alert("数据异常");
                getActivity().finish();
            }
        }
        return rootView;
    }

    private void initView() {
        mapView = (ZHMapView) rootView.findViewById(R.id.mapview);
        mapView.setOnMapLoadedCallback(this);
        mapView.showZoomControls(true);
        mapView.showScaleControl(true);
        mapView.showCompass(true);

        mGroupWhenDownPull = (Group) rootView.findViewById(R.id.group_when_down_pull);
        mGroupStoreinfo = (Group) rootView.findViewById(R.id.group_storeinfo);
        mTvName = (TextView) rootView.findViewById(R.id.tv_name);
        mTvDistance = (TextView) rootView.findViewById(R.id.tv_distance);
        mTvPhoneNumber = (TextView) rootView.findViewById(R.id.tv_phone_number);
        mTvAddress = (TextView) rootView.findViewById(R.id.tv_address);
        rootView.findViewById(R.id.iv_down_pull).setOnClickListener(this);
        mIvStore = (ImageView) rootView.findViewById(R.id.iv_store);
        rootView.findViewById(R.id.iv_up_pull).setOnClickListener(this);
    }

    private void initData() {
        mapPoint = new MapPointBean();
        if (mStoreInfo != null) {
            mGroupStoreinfo.setVisibility(View.VISIBLE);
            mTvName.setText(mStoreInfo.name);
            DecimalFormat df = new DecimalFormat("#.0");
            if (mStoreInfo.distance < 1000) {
                df = new DecimalFormat("0.0");
            }
            df.format(new BigDecimal(mStoreInfo.distance));
            mTvDistance.setText(df.format(new BigDecimal(((long) mStoreInfo.distance) * 1.0 / 1000)) + "km");
            LogUtils.i(mStoreInfo.distance, mTvDistance.getText().toString());
            LogUtils.i(mStoreInfo.toString());
            mTvPhoneNumber.setText(mStoreInfo.contactNumber);
            mTvAddress.setText(mStoreInfo.address);
            mapPoint.lat = ((double) mStoreInfo.lat) / 1000000;
            mapPoint.lon = ((double) mStoreInfo.lng) / 1000000;
        } else if (arguments.containsKey("lon") && arguments.containsKey("lat")) {
            mapPoint.lat = (arguments.getDouble("lat")) / 1000000;
            mapPoint.lon = (arguments.getDouble("lon")) / 1000000;
        }
        mapPoint.resId = R.mipmap.ic_mapmark;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onMapLoaded() {
        ZHMapUtils.centerScreen(mapView, mapPoint, 13);
        ZHMapUtils.addMarker(mapView, mapPoint);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_down_pull:
                mGroupWhenDownPull.setVisibility(View.VISIBLE);
                break;
            case R.id.iv_up_pull:
                mGroupWhenDownPull.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }
}
